package net.cloudcodex.client.main.view;

import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.DockLayoutPanel;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TabLayoutPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;

/**
 * 
 * 
 * @author ADaroussin
 */
public class MainViewImpl extends DockLayoutPanel implements MainView {

	private final Hyperlink profileHyperlink;
	private final Anchor logoutAnchor;
	private final TabLayoutPanel tabPanel = new TabLayoutPanel(30, Unit.PX);

	private final DialogBox dialogBox = new DialogBox(true, true);

	@Inject
	public MainViewImpl(Constant constant, Message message) {
		super(Unit.PX);

		this.addStyleName(constant.getMainViewStyle());
		
		dialogBox.setText(message.getDialogBoxTitle());

		final FlowPanel northPanel = new FlowPanel();
		northPanel.addStyleName(constant.getTitleStyle());

		logoutAnchor = new Anchor(message.getLinkLogout());
		logoutAnchor.addStyleName(constant.getHeaderLinkStyle());
		northPanel.add(logoutAnchor);

		profileHyperlink = new Hyperlink(message.getLinkProfile(), constant.getLinkProfile());
		profileHyperlink.addStyleName(constant.getHeaderLinkStyle());
		northPanel.add(profileHyperlink);

		this.addNorth(northPanel, 60);
		this.add(tabPanel);
	}

	@Override
	public HasClickHandlers getLogoutLink() {
		return logoutAnchor;
	}

	@Override
	public void addTab(IsWidget tab, String tabName) {
		final Widget widget = tab.asWidget();
		tabPanel.add(widget, tabName);
		widget.setVisible(true);
	}

	@Override
	public void selectTab(IsWidget tab) {
		tabPanel.selectTab(tab);
	}

	@Override
	public void displayAlertMessage(String message) {
		dialogBox.setWidget(new Label(message));
		dialogBox.center();
	}
}
